import { actions } from "../../http/actions";
import { hc } from "../../hc";
import BaseMQTTService from "../BaseService";

export default class HCMQTTPersonService extends BaseMQTTService {

	/**
	 * 下发或修改名单，通过picBase64下发照片
	 */
	async editPersonByBase64(topic: string, info: hc.mqtt.editPerson["info"], picBase64: string) {
		const operator = "EditPerson";

		const data = { operator, info: { ...info, pic: picBase64 } };
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.editPersonAndDelPersonAck>(topic, data);
		return res;
	}

	/**
	 * 下发或修改名单，通过 url下发照片
	 */
	async editPersonByURL(topic: string, info: hc.mqtt.editPerson["info"], url: string) {
		const operator = "EditPerson";

		const data = { operator, info: { ...info, picURI: url } };
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.editPersonAndDelPersonAck>(topic, data);
		return res;
	}

	/** 批量下发或修改名单 根据customId有则修改无则添加
	 *
	 * 最多1000条记录
	 *
	 * 只能是url下发照片，不能携带base64
	 *
	 * 超时时间设置为20分钟，网络没问题情况下，可保证1000条按每s一条的数据下发完成并收到响应，但不是100%可靠
	 */
	async batchAddEditPersons(topic: string, info: hc.mqtt.editPerson["info"][]) {
		const operator = actions.EditPersonsNew;
		const data = {
			messageId: "",
			DataBegin: "BeginFlag",
			operator,
			PersonNum: info.length,
			info,
			DataEnd: "EndFlag",
		};
		const client = this.getMQTTClient();

		// 默认设置20分钟延迟，20分钟内肯定已经处理完最多1000条数据
		const res = await client.fetch<hc.mqtt.batchAddEditPersonsAck>(topic, data, { qos: 1 }, 1000 * 60 * 20);
		return res;
	}

	/** 查询批量新增或修改状态
	 * 
	 * 用于批量下发名单时，中途查询下发状态
	 */
	async queryBatchAddEditStatus(topic: string) {
		const operator = "QueryProgress";
		const data = { operator, info: {} };
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.queryBatchAddEditStatusAck>(topic, data);
		return res;
	}
	/**
	 * 删除人员名单 单个 通过customId
	 */
	async delPerson(topic: string, customId: string) {
		const operator = "DelPerson";

		const data = { operator, info: { customId } };
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.editPersonAndDelPersonAck>(topic, data);
		return res;
	}

	/** 获取全部名单的customId列表 */
	async getAllPersonCustomId(topic: string) {
		const operator = "QueryPerson";
		const data = { operator, info: {} };
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.QueryPersonAck>(topic, data);
		return res;
	}

	/**
	 * 查询单个人员名单详细信息
	 *
	 * @param {string} topic
	 * @param {string} customId 自定义id
	 * @param {boolean} [pic=false] 是否包含照片
	 * @memberof HCMQTTPersonService
	 */
	async getOnePersonDetailInfo(topic: string, customId: string, pic = false) {
		const data = {
			operator: "SearchPerson",
			info: {
				customId,
				Picture: pic ? "1" : "0",
			},
		};
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.SearchPersonAck>(topic, data);
		return res;
	}

	/** 删除所有人员名单
	 *
	 * 注意：删除所有名单将清理所有控制记录，且不可恢复！
	 * 将会使设备重启
	 */
	async deleteAllPerson(topic: string) {
		const operator = actions.DeleteAllPerson;
		const data = {
			operator,
			info: {
				deleteall: "1",
			},
		};
		const ctx = this.app.currentContext || {}
		this.app.logger.warn(`执行海清删除所有名单操作 %s %s`, topic, ctx?.getUserId() || "system");
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.deleteAllPersonAck>(topic, data);
		return res;
	}
}
